package com.sf.appointment.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.sf.appointment.domain.BusAppointment;
import com.sf.appointment.mapper.BusAppointmentMapper;
import com.sf.appointment.utils.RegexUtils;
import com.sf.appointment.utils.VehiclePlateNoUtil;
import com.sf.appointment.vo.BusStatementVo;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SecurityUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sf.appointment.mapper.BusStatementMapper;
import com.sf.appointment.domain.BusStatement;
import com.sf.appointment.service.IBusStatementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-23
 */
@Transactional
@Service
public class BusStatementServiceImpl implements IBusStatementService
{
    @Autowired
    private BusStatementMapper busStatementMapper;

    @Autowired
    private BusAppointmentMapper busAppointmentMapper;





    /**
     * 查询结算单
     *
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id)
    {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     *
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement)
    {
        if (busStatement.getParams().get("beginActualArrivalTime")!=null&&busStatement.getParams().get("beginActualArrivalTime")!=" "&&busStatement.getParams().get("endActualArrivalTime")!=null&&busStatement.getParams().get("endActualArrivalTime")!=" "&&busStatement.getParams().get("beginActualArrivalTime").equals(busStatement.getParams().get("endActualArrivalTime"))){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate endDate = LocalDate.parse((String)(busStatement.getParams().get("endActualArrivalTime")), formatter);
            // 将结束日期加一天
            LocalDate newEndDate = endDate.plusDays(1);
            String newEndDateStr = newEndDate.format(formatter);
            busStatement.getParams().put("endActualArrivalTime", newEndDateStr);
        }
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     *
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int insertBusStatement(BusStatementVo busStatement)
    {
        Assert.notNull(busStatement.getCustomerName(),"客户姓名不能为空");
        Assert.notNull(busStatement.getCustomerPhone(),"联系方式不能为空");
        Assert.notNull(busStatement.getLicensePlate(),"车牌号码不能为空");
        Assert.notNull(busStatement.getCarSeries(),"汽车类型不能为空");
        Assert.notNull(busStatement.getServiceType(),"服务类型不能为空");

        busStatement.setCreateTime(DateUtils.getNowDate());
        Date createTime = busStatement.getCreateTime();
        boolean phoneLegal = RegexUtils.isPhoneLegal(busStatement.getCustomerPhone());
        if(!phoneLegal){
            throw new IllegalArgumentException("手机号码不正确");
        }

        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new IllegalArgumentException("车牌号不正确");
        }






        BusStatement bus = new BusStatement();
        BeanUtils.copyProperties(busStatement,bus);
        bus.setActualArrivalTime(createTime);
        return busStatementMapper.insertBusStatement(bus);
    }

    /**
     * 修改结算单
     *
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatementVo busStatement)
    {

        Assert.notNull(busStatement.getCustomerName(),"客户姓名不能为空");
        Assert.notNull(busStatement.getCustomerPhone(),"联系方式不能为空");
        Assert.notNull(busStatement.getLicensePlate(),"车牌号码不能为空");
        Assert.notNull(busStatement.getCarSeries(),"汽车类型不能为空");
        Assert.notNull(busStatement.getServiceType(),"服务类型不能为空");



        boolean phoneLegal = RegexUtils.isPhoneLegal(busStatement.getCustomerPhone());
        if(!phoneLegal){
            throw new IllegalArgumentException("手机号码不正确");
        }

        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new IllegalArgumentException("车牌号不正确");
        }

        BusStatement busStatement1 = busStatementMapper.selectBusStatementById(busStatement.getId());
        if(busStatement1==null){
            throw new RuntimeException("id不合法");
        }
        if(!busStatement1.getStatus().equals(BusStatement.STATUS_CONSUME)){
            throw new RuntimeException("操作不合法");
        }

        BusStatement bus = new BusStatement();
        BeanUtils.copyProperties(busStatement,bus);


        return busStatementMapper.updateBusStatement(bus);
    }

    /**
     * 批量删除结算单
     *
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids)
    {
        int flag=0;
        for(Long id:ids){
            int i = this.deleteBusStatementById(id);
            flag+=i;
        }
        return flag;
    }

    /**
     * 删除结算单信息
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id)
    {
        BusStatement busStatement = selectBusStatementById(id);
        if(busStatement==null){
            throw new RuntimeException("id不合法");
        }
        if (busStatement.getStatus().equals(BusStatement.STATUS_PAID)){
            throw new RuntimeException("操作不合法");
        }
        Long appointmentId = busStatement.getAppointmentId();
        if (appointmentId!=null){
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(appointmentId);
            if (busAppointment==null){
                throw new RuntimeException("操作出错");
            }
            busAppointment.setStatus(BusAppointment.STATUS_ARRIVAL);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }
        return busStatementMapper.deleteBusStatementById(id);
    }


}
